package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询今日佳人
    public RecommendUser queryWithMaxScore(Long toUserId) {

        //根据toUserId查询，根据评分score排序，获取第一条
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //调用mongoTemplate查询

        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    //查询今日推荐列表
    @Override
    public List<RecommendUser> recommendationList(Long toUserId) {
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria);
        //总记录数
        Long count = mongoTemplate.count(query, RecommendUser.class);
        //配置分页参数
        return mongoTemplate.find(query, RecommendUser.class);

    }

    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long userId) {
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(userId);
        //构建Query对象
        Query query = Query.query(criteria);
        //总记录数
        Long count = mongoTemplate.count(query, RecommendUser.class);
        //配置分页参数
        query.skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("score")));
        //查询数据列表
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        return new PageResult(page, pagesize, count, recommendUsers);
    }

    @Override
    public RecommendUser queryByUserId(Long recomuserId, Long userId) {
        Query query = Query.query(Criteria.where("userId").is(recomuserId).and("toUserId").is(userId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        //如果为null
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(userId);
            //构建缘分值
            recommendUser.setScore(95d);
        }

        return recommendUser;
    }

    /**
     * @param userId 登录用户id
     * @param count  限制每次查询数量
     * @return
     */
    @Override
    public List<RecommendUser> queryCardsList(Long userId, int count) {
        //1、查询喜欢不喜欢的用户ID  排除
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<Long> likeuserId = CollUtil.getFieldValues(userLikes, "likeuserId",Long.class);

        //2、构造查询推荐用户的条件
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeuserId);
        //3、使用统计函数，随机获取推荐的用户列表
        TypedAggregation<RecommendUser> newAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),//指定查询条件
                Aggregation.sample(count)
        );
        AggregationResults<RecommendUser> results = mongoTemplate.aggregate(newAggregation, RecommendUser.class);
        //4、构造返回
        return results.getMappedResults();

    }
}
